<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * @see \Smartwave\Porto\Block\System\Config\Form\Button
 */
?>
<script>
require(['prototype'], function(){

//<![CDATA[
    function import<?php echo $block->getDemoVersion(); ?>() {

        var resultMessage = $('import_result<?php echo $block->getDemoVersion(); ?>');
        params = {
            demo_version: '<?php echo $block->getDemoVersion(); ?>',
            current_store: ($('store_switcher'))?$('store_switcher').value:'',
            current_website: ($('website_switcher'))?$('website_switcher').value:''
        };

        new Ajax.Request('<?php echo $block->getAjaxUrl() ?>', {
            parameters: params,
            onSuccess: function(response) {
                var validationMessage = document.getElementById("import_result<?php echo $block->getDemoVersion(); ?>");
                var result = '<?php echo __('Error during Import '.$block->getDemoVersion().'.') ?>';
                try {
                    if (response.responseText.isJSON()) {
                        response = response.responseText.evalJSON();
                        result = response.message;
                    }
                    if (response.valid == 1) {
                        validationMessage.removeClassName('hidden').addClassName('success').addClassName('message-success');
                    } else {
                        validationMessage.removeClassName('hidden').addClassName('error').addClassName('message-error');
                    }
                } catch (e) {
                    validationMessage.removeClassName('hidden').addClassName('error').addClassName('message-error')
                }
                validationMessage.update(result);
                setTimeout(function(){
                    location.reload();
                },3000);
            }
        });
    }

    window.import<?php echo $block->getDemoVersion(); ?> = import<?php echo $block->getDemoVersion(); ?>;
//]]>

});
</script>
<style type="text/css">
#import_result<?php echo $block->getDemoVersion(); ?>:before{
    top: 20px;
    margin: 0;
}
</style>
<div class="actions actions-import-data">
    <button onclick="javascript:import<?php echo $block->getDemoVersion(); ?>(); return false;" class="action-import-data" type="button" id="<?php echo $block->getHtmlId()     ?>">
        <span><?php echo $block->escapeHtml($block->getButtonLabel()) ?></span>
    </button>
    <div id="import_result<?php echo $block->getDemoVersion(); ?>" class="message-validation hidden message"></div>
</div>
